package com.haojii.notifier.easytv;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.haojii.notifier.user.User;
import com.haojii.notifier.user.UserUtils;

@SuppressWarnings("serial")
public class EasytvServlet extends HttpServlet {

	private static final String EASYTV_NOTIFY_TASK_ADD_JSP = "easytv_notify_task_add_v2.jsp";
	private static final Logger log = Logger.getLogger(EasytvServlet.class.getName());

	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		List<EasytvEntity> list = new ArrayList<EasytvEntity>();

		UserService userService = UserServiceFactory.getUserService();
		String thisURL = req.getRequestURI();
		if (req.getUserPrincipal() != null) {
			com.google.appengine.api.users.User u = userService.getCurrentUser();
			User user = UserUtils.get(u.getEmail());
			if (user == null) {
				user = new User(u, "");
				UserUtils.save(user);
			} else {
				Set<Key> interestedEasytvEntities = user.getInterestedEasytvEntities();
				for (Key key : interestedEasytvEntities) {
					EasytvEntity easytvEntity = EasytvEntityUtils.get(KeyFactory.keyToString(key));
					list.add(easytvEntity);
				}
			}
			req.setAttribute("user", user);
			log.log(Level.INFO, "GET list.size()=" + list.size());
			req.setAttribute("easytvEntityList", list);

			req.setAttribute("LogoutURL", userService.createLogoutURL(thisURL));

		} else {
			req.setAttribute("LoginURL", userService.createLoginURL(thisURL));
		}

		req.getRequestDispatcher(EASYTV_NOTIFY_TASK_ADD_JSP).forward(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		UserService userService = UserServiceFactory.getUserService();
		com.google.appengine.api.users.User u = userService.getCurrentUser();
		User user = UserUtils.get(u.getEmail());

		String method = req.getParameter("method");

		if (method != null && method.equals("add")) {
			String easytv_url = req.getParameter("easytv_url").trim();
			String easytv_title = req.getParameter("easytv_title").trim();
			// EasytvEntity e = new EasytvEntity(easytv_title, easytv_url);

			EasytvEntity e = EasytvEntityUtils.get(easytv_url);
			if (e == null) {
				e = new EasytvEntity(easytv_title, easytv_url);
			}
			e.getWatchingUsers().add(user.getKey());
			user.getInterestedEasytvEntities().add(e.getKey());
			EasytvEntityUtils.save(e);
			UserUtils.save(user);
		}
		if (method != null && method.equals("delete")) {
			String easytv_url = req.getParameter("easytv_url").trim();
			if (user.getInterestedEasytvEntities() != null) {
				user.getInterestedEasytvEntities().remove(
								KeyFactory.createKey(EasytvEntity.class.getSimpleName(), easytv_url));
			}
			EasytvEntity e = EasytvEntityUtils.get(easytv_url);
			e.getWatchingUsers().remove(user.getKey());
			EasytvEntityUtils.update(e);
			UserUtils.update(user);
		}

		List<EasytvEntity> list = EasytvEntityUtils.list();
		log.log(Level.INFO, "POST list.size()=" + list.size());

		resp.sendRedirect("easytv");
	}

}
